﻿using Order;
using System;
using System.Windows.Forms;

namespace OrderForm
{
    public partial class ModifyOrder : Form
    {
        readonly Order.Order order;
        public ModifyOrder(Order.Order order)
        {
            InitializeComponent();
            this.order = order;
        }

        private void ModifyOrder_Load(object sender, EventArgs e)
        {
            bindingSource.DataSource = order.OrderDetails;
            detailView.DataSource = bindingSource;
            orderID.Text = order.OrderID;
            orderTime.Value = order.OrderTime;
            customerName.Text = order.TheCustomer.Name;
            address.Text = order.ShippingAddress.Location;
        }

        private void Submit_Click(object sender, EventArgs e)
        {
            order.OrderID = orderID.Text;
            order.OrderTime = orderTime.Value;
            order.TheCustomer.Name = customerName.Text;
            order.ShippingAddress.Location = address.Text;
            order.ComputeTotal();
            Close();
        }

        private void Add_Click(object sender, EventArgs e)
        {
            OrderDetail detail = new();
            if (new Detail(detail).ShowDialog(this) == DialogResult.OK)
            {
                bindingSource.Add(detail);
                //detailView.Refresh();
            }
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            if (detailView.SelectedRows.Count > 0 && detailView.SelectedRows[0].DataBoundItem is OrderDetail detail)
            {
                bindingSource.Remove(detail);
                //detailView.Refresh();
            }

        }
    }
}
